using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace sforceExplorer
{
	/// <summary>
	/// Summary description for properties.
	/// </summary>
	public class properties : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtBatchSize;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.TextBox txtClientID;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox txtProxy;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.TextBox txtEndpoint;
		private System.Windows.Forms.TextBox txtSessionId;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtProxyPort;
		private System.Windows.Forms.CheckBox chkUseSessionId;
		private System.Windows.Forms.TextBox txtAppServerURL;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.CheckBox cbNoHTTPS;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public properties()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.txtEndpoint.Text = GlobalProperties.endPoint;
			this.txtAppServerURL.Text = GlobalProperties.appServerURL;
			this.txtClientID.Text = GlobalProperties.clientId;
			this.txtProxy.Text = GlobalProperties.proxyServer;
			this.txtSessionId.Text = GlobalProperties.sessionId;
			this.txtProxyPort.Text = GlobalProperties.proxyPort;
			this.chkUseSessionId.Checked = GlobalProperties.useSessionId;
			this.cbNoHTTPS.Checked = GlobalProperties.noHTTPS;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.txtBatchSize = new System.Windows.Forms.TextBox();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.cbNoHTTPS = new System.Windows.Forms.CheckBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label3 = new System.Windows.Forms.Label();
			this.chkUseSessionId = new System.Windows.Forms.CheckBox();
			this.txtSessionId = new System.Windows.Forms.TextBox();
			this.txtAppServerURL = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.txtClientID = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.txtProxy = new System.Windows.Forms.TextBox();
			this.label7 = new System.Windows.Forms.Label();
			this.txtEndpoint = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtProxyPort = new System.Windows.Forms.TextBox();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.tabControl1.SuspendLayout();
			this.tabPage1.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.tabPage2.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(96, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Query batch size:";
			// 
			// txtBatchSize
			// 
			this.txtBatchSize.Location = new System.Drawing.Point(112, 16);
			this.txtBatchSize.MaxLength = 4;
			this.txtBatchSize.Name = "txtBatchSize";
			this.txtBatchSize.Size = new System.Drawing.Size(40, 20);
			this.txtBatchSize.TabIndex = 1;
			this.txtBatchSize.Text = "500";
			// 
			// btnOK
			// 
			this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnOK.Location = new System.Drawing.Point(360, 312);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(64, 24);
			this.btnOK.TabIndex = 2;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnCancel.Location = new System.Drawing.Point(432, 312);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(64, 24);
			this.btnCancel.TabIndex = 2;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// tabControl1
			// 
			this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.tabControl1.Controls.Add(this.tabPage1);
			this.tabControl1.Controls.Add(this.tabPage2);
			this.tabControl1.Location = new System.Drawing.Point(8, 8);
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(488, 288);
			this.tabControl1.TabIndex = 3;
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.Add(this.cbNoHTTPS);
			this.tabPage1.Controls.Add(this.groupBox1);
			this.tabPage1.Controls.Add(this.txtClientID);
			this.tabPage1.Controls.Add(this.label6);
			this.tabPage1.Controls.Add(this.label5);
			this.tabPage1.Controls.Add(this.txtProxy);
			this.tabPage1.Controls.Add(this.label7);
			this.tabPage1.Controls.Add(this.txtEndpoint);
			this.tabPage1.Controls.Add(this.label2);
			this.tabPage1.Controls.Add(this.txtProxyPort);
			this.tabPage1.Location = new System.Drawing.Point(4, 22);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(480, 262);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "Login";
			// 
			// cbNoHTTPS
			// 
			this.cbNoHTTPS.Location = new System.Drawing.Point(16, 112);
			this.cbNoHTTPS.Name = "cbNoHTTPS";
			this.cbNoHTTPS.Size = new System.Drawing.Size(208, 16);
			this.cbNoHTTPS.TabIndex = 13;
			this.cbNoHTTPS.Text = "Do not require HTTPS";
			// 
			// groupBox1
			// 
			this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.chkUseSessionId);
			this.groupBox1.Controls.Add(this.txtSessionId);
			this.groupBox1.Controls.Add(this.txtAppServerURL);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Location = new System.Drawing.Point(8, 144);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(464, 112);
			this.groupBox1.TabIndex = 12;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "WIL Emulation";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 56);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 16);
			this.label3.TabIndex = 12;
			this.label3.Text = "Sess. Id";
			// 
			// chkUseSessionId
			// 
			this.chkUseSessionId.Location = new System.Drawing.Point(16, 24);
			this.chkUseSessionId.Name = "chkUseSessionId";
			this.chkUseSessionId.Size = new System.Drawing.Size(168, 16);
			this.chkUseSessionId.TabIndex = 10;
			this.chkUseSessionId.Text = "Use Session Id";
			// 
			// txtSessionId
			// 
			this.txtSessionId.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtSessionId.Location = new System.Drawing.Point(72, 48);
			this.txtSessionId.Name = "txtSessionId";
			this.txtSessionId.Size = new System.Drawing.Size(384, 20);
			this.txtSessionId.TabIndex = 11;
			this.txtSessionId.Text = "";
			// 
			// txtAppServerURL
			// 
			this.txtAppServerURL.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtAppServerURL.Location = new System.Drawing.Point(72, 80);
			this.txtAppServerURL.Name = "txtAppServerURL";
			this.txtAppServerURL.Size = new System.Drawing.Size(384, 20);
			this.txtAppServerURL.TabIndex = 11;
			this.txtAppServerURL.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 80);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 16);
			this.label4.TabIndex = 12;
			this.label4.Text = "Serv. URL";
			// 
			// txtClientID
			// 
			this.txtClientID.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtClientID.Location = new System.Drawing.Point(88, 80);
			this.txtClientID.Name = "txtClientID";
			this.txtClientID.Size = new System.Drawing.Size(384, 20);
			this.txtClientID.TabIndex = 8;
			this.txtClientID.Text = "";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(8, 88);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(80, 16);
			this.label6.TabIndex = 6;
			this.label6.Text = "Client Id:";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 40);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(80, 16);
			this.label5.TabIndex = 4;
			this.label5.Text = "Proxy Host:";
			// 
			// txtProxy
			// 
			this.txtProxy.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtProxy.Location = new System.Drawing.Point(88, 32);
			this.txtProxy.Name = "txtProxy";
			this.txtProxy.Size = new System.Drawing.Size(384, 20);
			this.txtProxy.TabIndex = 9;
			this.txtProxy.Text = "";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(8, 16);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(80, 16);
			this.label7.TabIndex = 5;
			this.label7.Text = "Endpoint:";
			// 
			// txtEndpoint
			// 
			this.txtEndpoint.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtEndpoint.Location = new System.Drawing.Point(88, 8);
			this.txtEndpoint.Name = "txtEndpoint";
			this.txtEndpoint.Size = new System.Drawing.Size(384, 20);
			this.txtEndpoint.TabIndex = 7;
			this.txtEndpoint.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(80, 16);
			this.label2.TabIndex = 4;
			this.label2.Text = "Proxy Port:";
			// 
			// txtProxyPort
			// 
			this.txtProxyPort.Location = new System.Drawing.Point(88, 56);
			this.txtProxyPort.Name = "txtProxyPort";
			this.txtProxyPort.Size = new System.Drawing.Size(48, 20);
			this.txtProxyPort.TabIndex = 9;
			this.txtProxyPort.Text = "";
			// 
			// tabPage2
			// 
			this.tabPage2.Controls.Add(this.txtBatchSize);
			this.tabPage2.Controls.Add(this.label1);
			this.tabPage2.Location = new System.Drawing.Point(4, 22);
			this.tabPage2.Name = "tabPage2";
			this.tabPage2.Size = new System.Drawing.Size(480, 262);
			this.tabPage2.TabIndex = 1;
			this.tabPage2.Text = "Query";
			// 
			// properties
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(504, 350);
			this.ControlBox = false;
			this.Controls.Add(this.tabControl1);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.btnCancel);
			this.Name = "properties";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Options";
			this.Load += new System.EventHandler(this.properties_Load);
			this.tabControl1.ResumeLayout(false);
			this.tabPage1.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.tabPage2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void properties_Load(object sender, System.EventArgs e)
		{
			this.txtBatchSize.Text = Application.UserAppDataRegistry.GetValue("batchSize", "500").ToString();
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			if (this.chkUseSessionId.Checked) 
			{
				if (!GlobalProperties.CheckSessionId(this.txtAppServerURL.Text, this.txtSessionId.Text)) 
				{
					System.Windows.Forms.MessageBox.Show("The session id you entered is not valid.  Please enter a valid session id or uncheck the Use Session Id checkbox.", "Session Id Not Valid", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
					return;
				}
			}

			if (Convert.ToInt16(this.txtBatchSize.Text) < 2001 && Convert.ToInt16(this.txtBatchSize.Text) > 1) 
				Application.UserAppDataRegistry.SetValue("batchSize", this.txtBatchSize.Text);
			else 
			{
				System.Windows.Forms.MessageBox.Show("The batch size must be between for 2 to 2000.", "Value out range", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
				return;
			}
			GlobalProperties.endPoint = this.txtEndpoint.Text;
			GlobalProperties.appServerURL = this.txtAppServerURL.Text;
			GlobalProperties.sessionId = this.txtSessionId.Text;
			GlobalProperties.clientId = this.txtClientID.Text;
			GlobalProperties.proxyPort = this.txtProxyPort.Text;
			GlobalProperties.proxyServer = this.txtProxy.Text;
			GlobalProperties.useSessionId = this.chkUseSessionId.Checked;
			GlobalProperties.noHTTPS = this.cbNoHTTPS.Checked;
			GlobalProperties.SaveSettings();
			this.Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
